//
//  ZYNTableViewCellForNewSong.m
//  VideoPic
//
//  Created by dllo on 16/3/23.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "ZYNTableViewCellForNewSong.h"
#import "ZYNTableViewCellForTableViewCellForNewSong.h"
#import "ZYNModelForHotSongs.h"
#define WIDTH self.frame.size.width
@interface ZYNTableViewCellForNewSong()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSMutableArray *arrForTing;
@property (nonatomic, strong)NSMutableArray *arrForCount;
@property (nonatomic, assign) NSInteger index;
@end
@implementation ZYNTableViewCellForNewSong

#pragma mark - init
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self.arrForTing = [NSMutableArray array];
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.index = -1;
        [self createSubViews];
    
    }
    return self;
}

#pragma mark - createSubViews
- (void)createSubViews {
    //cell style
    self.selectionStyle = UITableViewCellSelectionStyleNone;
    
    self.tableView = [[UITableView alloc] initWithFrame:self.contentView.frame style:UITableViewStylePlain];
    
    [self.contentView addSubview:self.tableView];
   
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.scrollEnabled = NO;
    
    [self.tableView registerClass:[ZYNTableViewCellForTableViewCellForNewSong class] forCellReuseIdentifier:@"pool1"];

}

#pragma mark - layout
- (void)layoutSubviews {
    [super layoutSubviews];
    self.tableView.frame = self.contentView.frame;
    
}
#pragma mark - UITableView delegate methods
#pragma mark ** two request dataSoure delegate methods
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

    return self.arrForTing.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    ZYNTableViewCellForTableViewCellForNewSong *cell = [tableView dequeueReusableCellWithIdentifier:@"pool1"];
    
    ZYNModelForHotSongs *model = [self.arrForTing objectAtIndex:indexPath.row];
    
    [cell passModel:model];
    
    cell.labelOfCount.text = [NSString stringWithFormat:@"%ld",indexPath.row+1];
//    [self.tableView reloadData];
   
//    if (cell.index == cell.index) {
//        [cell.buttonForPlayOrPause setImage:[UIImage imageNamed:@"pausegray"] forState:UIControlStateNormal];
//    } else {
//        [cell.buttonForPlayOrPause setImage:[UIImage imageNamed:@"playgray"] forState:UIControlStateNormal];
//
//    }
    
    if (indexPath.row == self.index) {
        [cell.buttonForPlayOrPause setBackgroundImage:[UIImage imageNamed:@"pausegray"] forState:UIControlStateNormal];
    } else {
        [cell.buttonForPlayOrPause setBackgroundImage:[UIImage imageNamed:@"playgray"] forState:UIControlStateNormal];
        
        
    }
    
    [cell.buttonForPlayOrPause addTarget:self action:@selector(handlePic:) forControlEvents:UIControlEventTouchUpInside];
    
    cell.buttonForPlayOrPause.tag = indexPath.row;
    
    return cell;
}

- (void)handlePic:(UIButton *)button {
    self.index = button.tag;
    [self.tableView reloadData];

}
#pragma mark ** other system delegate methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
 
    return 1;
}

//height for cell
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    return WIDTH / 6;
    
}

#pragma mark - deliverModel
- (void)passArray:(NSMutableArray *)arr {
    self.arrForTing = arr;
   
}

- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
